package com.pay.game.central.security.service;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.pay.game.central.system.bean.User;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 *  生成token签名
 */
@Service("TokenService")
public class TokenService {
    public String getToken(User user) {
        String token="";
        Date date = new Date();
        date.setTime(System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 31 * 2);
        token= JWT.create().withAudience(user.getId())// 将 user id 保存到 token 里面
                .withExpiresAt(date)
                .sign(Algorithm.HMAC256(user.getPassword()));// 以 password 作为 token 的密钥
        return token;
    }
}